//
//  ZKHomeTableViewCell.swift
//  Weibo
//
//  Created by 郑克哥哥 on 2017/7/28.
//  Copyright © 2017年 郑克哥哥. All rights reserved.
//

import UIKit
import SnapKit

//自定义微博首页cell
class ZKHomeTableViewCell: UITableViewCell {
    
    //记录toolBar的顶部约束
    var toolBarTopConstraint: Constraint?
    
    
    
    //引用ViewModel
    var statusViewModel:ZKStatusViewModel?{
        didSet{
            originalView.statusViewModel = statusViewModel
            
            //重新创建toolBar底部约束之前,把之前的顶部约束卸载掉,保证toolBar顶部约束只有一条
            toolBarTopConstraint?.deactivate()
            if statusViewModel?.status?.retweeted_status != nil {
                
                //转发微博内容不为空
                transmitView.isHidden = false
                //重新创建toolBar顶部约束
                toolBarView.snp.makeConstraints({ (make) in
                    //重新记录新创建的toolBar顶部约束
                   toolBarTopConstraint = make.top.equalTo(transmitView.snp.bottom).constraint
                })
                //绑定内容
                transmitView.transmitModel = statusViewModel
            }else{
                
                //转发微博内容为空
                transmitView.isHidden = true
                //重新创建toolBar顶部约束
                toolBarView.snp.makeConstraints({ (make) in
                    toolBarTopConstraint = make.top.equalTo(originalView.snp.bottom).constraint
                })
            }
        }
    }
    
    //加载控件
    //原创微博视图
    fileprivate lazy var originalView: HomeOriginalView = HomeOriginalView()
    //转发微博视图
    fileprivate lazy var transmitView:HomeTransmitView = HomeTransmitView()
    //微博toolBar
    fileprivate lazy var toolBarView:HomeToolBarView = HomeToolBarView()
    //微博配图
    


    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        setupUI()
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //添加控件设置值约束
    private func setupUI (){
        //white: 0.95, alpha: 1:表示设置灰色
        contentView.backgroundColor = UIColor(white: 0.95, alpha: 1)
        contentView.addSubview(originalView)
        contentView.addSubview(transmitView)
        contentView.addSubview(toolBarView)
        
        originalView.snp.makeConstraints { (make) in
            make.top.equalTo(contentView).offset(8)
            make.leading.trailing.equalTo(contentView)
        }
        transmitView.snp.makeConstraints { (make) in
            make.top.equalTo(originalView.snp.bottom)
            make.leading.trailing.equalTo(originalView)
            
        }
        toolBarView.snp.makeConstraints { (make) in
            
            //重新记录新创建的toolBar顶部约束
            toolBarTopConstraint = make.top.equalTo(transmitView.snp.bottom).constraint
            make.leading.trailing.equalTo(transmitView)
            make.height.equalTo(35)
            
            make.bottom.equalTo(contentView)
        }
        
    
    }

}


